package com.schooleducation.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.schooleducation.entity.StuDepartment;
import com.schooleducation.entity.StuMajor;
import com.schooleducation.entity.User;
import com.schooleducation.service.ServiceFactory;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
@RequestMapping("admin/studentMajor")
public class StudentMajorController {

	@Resource(name = "serviceFac")
	private ServiceFactory serviceFac;

	/**
	 * 获取专业管理界面
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String getPage(HttpSession session, Model model) {
		User teacher = (User) session.getAttribute("admin");
		// 检查是否登录
		if (teacher != null) {
			List<StuDepartment> sds = serviceFac.getUserService().getStuDepartmentList();
			List<StuMajor> sms = serviceFac.getUserService().getStuMajorListByDepartment(sds.get(0).getId());

			model.addAttribute("admin", teacher);
			model.addAttribute("department", sds);
			model.addAttribute("major", sms);

			return "admin/studentMajor";
		} else {
			return "redirect:../login";
		}
	}
	
	/**
	 * 提交查询相应系别专业请求
	 * @param departmentId
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getStudentMajor(int departmentId) {
		List<StuMajor> ltts = serviceFac.getUserService().getStuMajorListByDepartment(departmentId);
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { "stuDepartment" });

		return JSONArray.fromObject(ltts, config).toString();
	}
	
	/**
	 * 更新专业和系别请求
	 * @param session
	 * @param data
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "updateMajor", method = RequestMethod.POST)
	@ResponseBody
	public String updateType(HttpSession session, String data, String type) {
		Map<String, Object> state = new HashMap<String, Object>();
		User admin = (User) session.getAttribute("admin");
		//验证是否登录
		if (admin != null) {
			state.put("isLogin", "yes");
			//选择更新类型
			if (type.equals("oneType")) {
				StuDepartment lot = (StuDepartment) JSONObject.toBean(JSONObject.fromObject(data), StuDepartment.class);
				//更新系别
				if (serviceFac.getUserService().updateStuDepartment(lot)) {
					state.put("update", "success");
				} else {
					state.put("update", "fail");
				}
			} else if (type.equals("twoType")) {
				StuMajor ltt = (StuMajor) JSONObject.toBean(JSONObject.fromObject(data), StuMajor.class);
				//更新专业
				if (serviceFac.getUserService().updateStuMajor(ltt)) {
					state.put("update", "success");
				} else {
					state.put("update", "fail");
				}
			}
		} else {
			state.put("isLogin", "no");
		}
		return JSONObject.fromObject(state).toString();
	}
	
	/**
	 * 删除学生专业和系别请求
	 * @param session
	 * @param typeId
	 * 		专业Id/系别Id
	 * @param type
	 * 		删除选择 oneType	twoType
	 * @return
	 */
	@RequestMapping(value = "deleteMajor", method = RequestMethod.POST)
	@ResponseBody
	public String deleteType(HttpSession session, int typeId, String type) {
		Map<String, Object> state = new HashMap<String, Object>();
		User admin = (User) session.getAttribute("admin");
		//验证是否登录
		if(admin != null) {
			state.put("isLogin", "yes");
			//选择删除类型
			if(type.equals("oneType")) {
				//删除系别
				if(serviceFac.getUserService().deleteStuDepartment(typeId)) {
					state.put("del", "success");
				} else {
					state.put("del", "fail");
				}
			} else if(type.equals("twoType")) {
				//删除专业
				if(serviceFac.getUserService().deleteStuMajor(typeId)) {
					state.put("del", "success");
				} else {
					state.put("del", "fail");
				}
			}
		} else {
			state.put("isLogin", "no");
		}
		return JSONObject.fromObject(state).toString();
	}
}
